﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class DicItemManagement : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);

            if (!ReportHelper.IsAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            }
            else if (!this.IsPostBack)
            {
                this.btnadd.Attributes.Add("onclick", "javascript:ShowPage(-1);");

                BaseFunction.BindDicTypeDropDownList(DicTypeDownList, string.Empty, true);

                this.GetData(this.DicTypeDownList.SelectedValue);
            }

            if (returnvalue != null && returnvalue.Value != "")
            {
                this.GetData(this.DicTypeDownList.SelectedValue);
                this.returnvalue.Value = "";
            }
        }

        protected void GetData(string filter)
        {
            try
            {
                DataTable dt = new DataTable();
                BEGDC.PreContract.BLL.IDicItem dicItem = (BEGDC.PreContract.BLL.IDicItem)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicItem_Service];
                if (0 == int.Parse(filter))
                    dt = dicItem.GetDicItemAndDicType_DataTable(string.Empty);
                else
                    dt = dicItem.GetDicItemAndDicType_DataTable("dicitem.dictypeid=" + filter);

                this.gvdicitem.DataSource = dt;
                this.gvdicitem.DataBind();
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void gvdicitem_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                ImageButton imgedit = (ImageButton)e.Row.FindControl("imgedit");
                imgedit.Attributes.Add("onclick", "javascript:ShowPage('" + this.gvdicitem.DataKeys[e.Row.RowIndex].Value.ToString() + "');");
            }
        }

        protected void DicTypeDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.GetData(this.DicTypeDownList.SelectedValue);
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        /// <summary>
        /// Bind dicType DropDownList
        /// </summary>
        protected void BindDPDicType()
        {
            try
            {
                BEGDC.PreContract.BLL.IDicType iDicType = (BEGDC.PreContract.BLL.IDicType)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicType_Service];
                DataTable dt = iDicType.GetDicType_DataTable(string.Empty);

                this.DicTypeDownList.DataSource = dt.DefaultView;
                this.DicTypeDownList.DataValueField = "DicTypeID";
                this.DicTypeDownList.DataTextField = "DicTypeDesc";
                this.DicTypeDownList.DataBind();
                this.DicTypeDownList.Items.Insert(0, new ListItem("All", "0"));
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

    }
}